<template>
	<view id="withdraw-page">
		<view class="page-header">
			<view class="nav-header-tab active" style="width:100%">
				<view class="header-title">提现</view>
			</view>
		</view>
		<view class="page-content">
			<view class="withdraw-box">
				<view class="title">可提现本金({{merchantMoneyText}}) ：{{canMoney}}</view>
				<view class="input-box">
					<input class="input" type="text" v-model="canMoneyFocus" @input="onInputCanMoney" />
				</view>

                <view class="title">可提现佣金({{merchantMoneyText}}) ：{{comCanMoney}}</view>
                <view class="input-box">
                    <input class="input" type="text" v-model="comCanMoneyFocus" @input="onInputComCanMoney" />
                </view>
                <view class="balance">
                    <view class="detail-btn" @click="allCanMoney">全部提现</view>
                </view>

				<view class="balance">
					<view class="balance-num">提现金额({{merchantMoneyText}}) ：{{totalFocus}}</view>
					<view class="detail-btn" @click="toDetailPage">提现明细</view>
				</view>
                <view class="balance">
				    <view class="balance-num" >提现钱包：{{winthdrawType}}</view>
                </view>
				<button class="confirm-btn" :loading="loading" :disabled="loading" @click="confirm">提现</button>
			</view>
			<view class="explanation-box">
				<view class="title">提现说明</view>
				<view class="content">1.提现金额最低{{lowest}},最高{{highest}}</view>
				<view class="content">2.当天提现额度{{dayMaxMoney}}，当天提现次数{{withdrewMaxNum}}次</view>
				<view class="content">3.申请提现后会马上到您的微信或支付宝钱包</view>
				<view class="content">4.提现打款至微信或支付宝钱包（需要已通过银行卡实名认证）</view>
				<view class="content">5.默认 {{aliPayMin}}-{{aliPayMax}} 提现到支付宝钱包</view>
				<view class="content">6.默认 {{wxPayMin}}-{{wxPayMax}} 提现到微信钱包</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
       apiCommissionCapitalCashAllWxAli,
       //apiPushSubmitWithdrawAllWxAli,
       pushAllWithdraw

	} from '../../../http/apis.js'

	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			...mapState(['token'])
		},
		data(){
			return {
				loading: false,
				bankStatus: 1,
				// wxStatus:1,
				// zfbStatus:1,
				// curClick: 'wx',
				withdrewType: 1,
				bankMinMoney: 0,
				bankMaxMoney: 0,
				lowest: 3,
				highest: 0,
				dayMaxMoney: 0,
				withdrawDayMaxMoneyed: 0,
				withdrawNum: 0,
				withdrewMaxNum: 0,
				canMoney: '0.00',// 可提现本金
				balance: '0.00', //余额
				money: '0.00', //提现金额,
                canMoneyFocus:'0.00',// 当前输入框本金
                comCanMoneyFocus:'0.00',// 当前输入框佣金
                totalFocus: '0.00', //当前输入框总金额
                comCanMoney:'0.00',// 可提现佣金
				moneyType: 1,
				merchantMoneyText: uni.getStorageSync('merchantMoneyText'),
				aliPayMax: '0.00', //支付宝上限
				wxPayMax: '0.00', //微信上限
				aliPayMin: '0.00', //支付宝下限
				wxPayMin: '0.00', //微信下限
				winthdrawType: '', //提现方式，微信|支付宝
				defaultPayType: '1', //1微信，2支付宝
				pushId: '',
			}
		},
		onShow() {
			this.getInitData()
		},
		onLoad(options) {
			this.moneyType = options.type
		},
		methods:{
            onInputCanMoney(e) {
                this.canMoneyFocus = e.detail.value
                this.totalFocus = this.numAdd(this.canMoneyFocus, this.comCanMoneyFocus)

                if (this.totalFocus > 0) {
                    if (parseInt(this.totalFocus) >= parseInt(this.aliPayMin) && parseInt(this.totalFocus) < parseInt(this.aliPayMax)) {
                        this.winthdrawType = '支付宝'
                        return
                    }
                    if(parseInt(this.totalFocus) >= parseInt(this.wxPayMin) && parseInt(this.totalFocus) < parseInt(this.wxPayMax)) {
                        this.winthdrawType = '微信'
                        return
                    }
                    if(this.defaultPayType == '1') {
                        this.winthdrawType = '微信'
                    }else{
                        this.winthdrawType = '支付宝'
                    }
                }else{
                    this.winthdrawType = ''
                }
            },
            onInputComCanMoney(e) {
                this.comCanMoneyFocus = e.detail.value
                this.totalFocus = this.numAdd(this.canMoneyFocus, this.comCanMoneyFocus)

                if (this.totalFocus > 0) {
                    if (parseInt(this.totalFocus) >= parseInt(this.aliPayMin) && parseInt(this.totalFocus) < parseInt(this.aliPayMax)) {
                        this.winthdrawType = '支付宝'
                        return
                    }
                    if(parseInt(this.totalFocus) >= parseInt(this.wxPayMin) && parseInt(this.totalFocus) < parseInt(this.wxPayMax)) {
                        this.winthdrawType = '微信'
                        return
                    }
                    if(this.defaultPayType == '1') {
                        this.winthdrawType = '微信'
                    }else{
                        this.winthdrawType = '支付宝'
                    }
                }else{
                    this.winthdrawType = ''
                }
            },
			getInitData(){
                apiCommissionCapitalCashAllWxAli({
					token: this.token,
					// type: this.moneyType
				}).then(res =>{
				    if(res.data.code == 1)  {
						this.balance = res.data.msg.money;
						this.lowest = res.data.msg.min_money;
						this.highest = res.data.msg.max_money
						this.dayMaxMoney = parseInt(res.data.msg.day_max_money)
						this.withdrawDayMaxMoneyed = parseInt(res.data.msg.withdrew_max_moneyed)
						this.withdrawNum = parseInt(res.data.msg.withdrew_num)
						this.withdrewMaxNum = parseInt(res.data.msg.withdrew_max)
						this.bankMinMoney = res.data.msg.bank_min_money
						this.bankMaxMoney = res.data.msg.bank_max_money
						this.aliPayMin = res.data.msg.aliPaymin
						this.aliPayMax = res.data.msg.aliPaymax
						this.wxPayMin = res.data.msg.weChatPaymin
						this.wxPayMax = res.data.msg.weChatPayMax
						this.defaultPayType = res.data.msg.defaultType
						this.pushId = res.data.msg.pushId
                        this.comCanMoney = res.data.msg.com_can_money
                        this.canMoney = res.data.msg.can_money
					}
				})
			},
            allCanMoney(){
                if (this.canMoney == 0 && this.comCanMoney == 0) {
                    this.$msg.showToast('当前没有可以全部提现的余额')
                    return
                }
                this.canMoneyFocus = parseFloat(this.canMoney).toFixed(2)
                this.comCanMoneyFocus = parseFloat(this.comCanMoney).toFixed(2)
                this.totalFocus = this.numAdd(this.canMoneyFocus, this.comCanMoneyFocus)

                if (this.totalFocus > 0) {
                    if (parseInt(this.totalFocus) >= parseInt(this.aliPayMin) && parseInt(this.totalFocus) < parseInt(this.aliPayMax)) {
                        this.winthdrawType = '支付宝'
                        return
                    }
                    if(parseInt(this.totalFocus) >= parseInt(this.wxPayMin) && parseInt(this.totalFocus) < parseInt(this.wxPayMax)) {
                        this.winthdrawType = '微信'
                        return
                    }
                    if(this.defaultPayType == '1') {
                        this.winthdrawType = '微信'
                    }else{
                        this.winthdrawType = '支付宝'
                    }
                }else{
                    this.winthdrawType = ''
                }
            },
            /**
             * 加法运算，佣金和本金相加为用户本次提现总金额
             * @param num1
             * @param num2
             * @returns {number}
             */
            numAdd(num1, num2) {
                var baseNum, baseNum1, baseNum2;
                try {
                    baseNum1 = num1.toString().split(".")[1].length;
                } catch (e) {
                    baseNum1 = 0;
                }
                try {
                    baseNum2 = num2.toString().split(".")[1].length;
                } catch (e) {
                    baseNum2 = 0;
                }
                baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
                return (num1 * baseNum + num2 * baseNum) / baseNum;
            },
			toDetailPage(){
				uni.navigateTo({
					url: './withdrawRecord'
				})
			},
            confirm(){
                if(this.totalFocus == '0.00' || parseInt(this.totalFocus) < 0){
                    this.$msg.showToast('当前没有余额可以提现')
                    return
                }

                if(parseFloat(this.comCanMoneyFocus) < 0 || parseFloat(this.canMoneyFocus) < 0){
                    this.$msg.showToast('金额填写错误')
                    return
                }

                if (parseFloat(this.totalFocus) < parseFloat(this.lowest)) {
                    this.$msg.showToast('提现金额最低' + this.lowest)
                    return
                }
                if (parseFloat(this.totalFocus) > parseFloat(this.highest)) {
                    this.$msg.showToast('提现金额最高' + this.highest)
                    return
                }
                if (this.withdrawDayMaxMoneyed > this.dayMaxMoney) {
                    this.$msg.showToast('当天提现额度已用完')
                    return
                }
                if (this.withdrawNum > this.withdrewMaxNum) {
                    this.$msg.showToast('当天提现次数已用完')
                    return
                }
                if(parseFloat(this.canMoneyFocus) > this.canMoney){
                    this.$msg.showToast('本金不足')
                    return
                }
                if(parseFloat(this.comCanMoneyFocus) > this.comCanMoney){
                    this.$msg.showToast('佣金不足')
                    return
                }
                uni.showLoading({
                    title: '提交中'
                });
                this.loading = true
                pushAllWithdraw({
                    token: this.token,
                    canMoney: this.canMoney,// 可提现本金
                    com_canMoney: this.comCanMoney,// 可提现佣金
                    com_money: parseFloat(this.comCanMoneyFocus).toFixed(2),// 提现佣金
                    money: parseFloat(this.canMoneyFocus).toFixed(2)// 提现本金
                }).then(res =>{
                    uni.hideLoading()
                    this.loading = false
                    if(res.data.code == 1)  {
                        this.$msg.showToast(res.data.msg)
                        setTimeout(()=>{
                            uni.navigateTo({
                                url:'./withdrawRecord'
                            })
                        },300)
                    } else {
                        if (res.data.msg == '用户未绑定支付宝账号') {
                            uni.navigateTo({
                                url: '/pages/bindInfo/bindAlipayCode?type=2&pushId=' + this.pushId
                            });
                        }else{
                            this.$msg.showToast(res.data.msg)
                        }
                    }
                }).catch(res => {
                    uni.hideLoading()
                    this.loading = false
                    this.$msg.showToast(res.data.msg)
                })
            }
		}
	}
</script>

<style lang="scss">
	page{
		background: #f5f5f5;
	}
	#withdraw-page{
		.page-header {
			text-align: center;
			font-size: $font-base;
			border-bottom: $border-bottom-sm;
			padding-top: 10upx;
			background: #FFFFFF;
			.nav-header-tab {
				display: inline-block;
				width: 50%;
				text-align: center;
				border-bottom: 2px solid transparent;
				padding: 5px 0;
				&.active {
					border-bottom: 2px solid $theme-color;
					.header-title {
						color:$theme-color;
						padding: 20upx 0;
					}
				}
			}
			.header-title {

			}
			.header-tips {
				color: $font-color-disabled;
				margin-top: 10upx;
			}
		}
		.page-content {
			.withdraw-box {
				background: #FFFFFF;
				border-radius: 24upx;
				padding: 32upx;
				margin: 32upx;
				.title {
					font-size: 28upx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 40upx;
				}
				.input-box {
					border-bottom: 1px solid #e0e0e0;
					margin-top: 20upx;
					display: flex;
					align-items: center;
					padding-bottom: 30upx;
					.symbol {
						font-size: 46upx;
						font-family: PingFangSC-Medium, PingFang SC;
						font-weight: 500;
						color: #333333;
						line-height: 56upx;
					}
					.input {
						width: 80%;
						padding: 5px;
						vertical-align: bottom;
						flex: 1;
						font-size: 36upx;
					}
					.all-withdraw {
						font-size: 28upx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: $theme-color;
						line-height: 24upx;
					}
				}
				.balance {
					display: flex;
					justify-content:space-between;

					margin-top: 32upx;
					.balance-num {
						font-size: 28upx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: #666666;
						line-height: 28upx;
					}
					.detail-btn {
						font-size: 28upx;
						font-family: PingFang-SC-Medium, PingFang-SC;
						font-weight: 500;
						color: $theme-color;
						line-height: 28upx;
					}
				}
				.confirm-btn {
					height: 80upx;
					line-height: 80upx;
					border-radius: 100upx;
					font-size: $font-lg;
					color: #fff;
					background: $theme-background-color;
					border-radius: 10upx;
					box-shadow: 1px 2px 5px $theme-color;
					margin: 60upx 32upx 30upx 32upx;
				}
			}
			.explanation-box {
				margin-top: 100upx;
				background: #FFFFFF;
				padding: 32upx;
				margin: 32upx;
				border-radius: 24upx;
				.title {
					font-size: 28upx;
					font-family: PingFangSC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 28upx;
					margin-bottom: 28upx;
				}
				.content {
					font-size: 28upx;
					font-family: PingFang-SC-Medium, PingFang-SC;
					font-weight: 400;
					color: #999999;
					line-height: 44upx;
				}
			}
		}
	}
</style>
